package com.mqtt;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

public class Server {
	public static final String HOST = "tcp://apollo-web.v.laikang.com:61613";
	
	private String userName = "test";
	private String passWord = "test";
	public static final String TOPIC = "TestTopic";
	private static final String clientid = "server";

	private MqttClient client;
	private MqttTopic topic;

	private MqttMessage message;

	private MqttConnectOptions options;

	public Server() throws MqttException {
		// MemoryPersistence设置clientid的保存形式，默认为以内存保存
		client = new MqttClient(HOST, clientid, new MemoryPersistence());
		connect();
	}

	private void connect() {
		options = new MqttConnectOptions();
		options.setCleanSession(false);
		options.setUserName(userName);
		options.setPassword(passWord.toCharArray());
		// 设置超时时间
		options.setConnectionTimeout(10);
		// 设置会话心跳时间
		options.setKeepAliveInterval(20);
		try {
			client.setCallback(new PushCallback());
			topic = client.getTopic(TOPIC);
			client.connect(options);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void publish(MqttMessage message) throws MqttPersistenceException, MqttException {
		MqttDeliveryToken token = topic.publish(message);
		token.waitForCompletion();
		System.out.println(token.isComplete() + "========");
	}

	public static void main(String[] args) throws MqttException {
		Server server = new Server();

		while (true) {

			server.message = new MqttMessage();
			server.message.setQos(1);
			server.message.setRetained(true);

			String uuid = "----" + Math.random() + "";
			server.message.setPayload(uuid.getBytes());
			server.publish(server.message);
			System.out.println(server.message.isRetained() + "------ratained状态，输出uuid：" + uuid);

			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}

	}
}
